clear all
*Define directory
cap cd "...\Replication pacakage\"

******************************************************
clear all
use data.dta
merge m:m DamagedPropertyStateCode using statelaw.dta
drop _merge

gen TotalBPDL= ApprovedAmountRealEstate+ ApprovedAmountContent
destring Year, replace force

keep if Year >2002

************************************************************

gen sample=3 //sample 3: 2014-2020
replace sample=2 if Year<=2013
replace sample=2 if Year<=2015 & FEMADisasterNumber =="" //sample 2: 2008-2013
replace sample=1 if Year<=2007 //sample 1: 2003-2007

*generate threshold
gen threshold=.
replace threshold=10000 if sample==1
replace threshold=14000 if sample==2
replace threshold=25000 if sample==3

*generate treatment dummy: UVTA staggered adoption. We use BPDL 2014-2020 sample since UVTA is introduced during the period. 
gen treat2=.
replace treat2=0 if sample==3 & Treatment==1 & Year<UVTA2014
replace treat2=0 if sample==3 & Treatment==0
*treat2 is the dummy for post-UVTA adoption
replace treat2=1 if sample==3 & Treatment==1 & Year>UVTA2014
tab treat2 

*************************************************************************************
*Keep loan amount between 1,000-65,000 and interest rate to 4% to be consistent with our bunching analysis sample
keep if TotalBPDL>1000 & TotalBPDL<65000 & PDBL==4

*We define take up ratio as the loan amount as a percentage of verified loss.
gen takeup=TotalBPDL/TotalVerifiedLoss
*We winsorize the take up ratio at 5% to avoid outliers.
winsor takeup, gen(wtakeup) p(0.05)

*We use $ 25,000 loss as a cut-off. For borrowers loan loss below $25,000 they have no incentives to bunch and would be more likely to have higher takeup ratio
gen loss25k=0
replace loss25k=1 if TotalVerifiedLoss>25000

***************DID**************
eststo clear
eststo: quietly reghdfe wtakeup treat2##loss25k , vce(cluster Year DamagedPropertyStateCode) noabsorb
eststo: quietly reghdfe wtakeup treat2##loss25k , vce(cluster Year DamagedPropertyStateCode) a(Year)
eststo: quietly reghdfe wtakeup treat2##loss25k , vce(cluster Year DamagedPropertyStateCode) a(DamagedPropertyStateCode)
eststo: quietly reghdfe wtakeup treat2##loss25k , vce(cluster Year DamagedPropertyStateCode) a(Year DamagedPropertyStateCode)
esttab * using "Table 4/Table 4.tex", ar2 b(3) se(3) star(* .10 ** .05 *** .01) noomitted nobase replace ///
title(DID analysis for state law change)
esttab, ar2 b(3) se(3) star(* .10 ** .05 *** .01) noomitted nobase

